﻿using System;
using System.Windows.Forms;
using CRMS.Views.DepartmentMgnt;
using CRMS.Views.CourseMgnt;
using CRMS.Views.TeacherMgnt;
using CRMS.Views.StudentMgnt;
using CRMS.Views.ClassroomMgnt;
using CRMS.Views.SelStudy;
using CRMS.Views.Helper;
using CRMS.Models;

namespace CRMS.Views
{
    public partial class frmMain : Form
    {
        private StudentRep studentRep = new StudentRep();
        //Constructor
        public frmMain()
        {
            InitializeComponent();
            CenterToScreen();
            fm = this;
            this.xytOne.Text = "操作员：" + userName;
            this.xytTwo.Text = "拥有权限：" + userRight;
        }
        //检查MdiChildren是否已经打开
        public bool CheckForm(string Name)
        {
            foreach (Form f in this.MdiChildren)
            {
                if (f.Name == Name)
                {
                    if (f.WindowState == FormWindowState.Minimized)
                    {
                        f.WindowState = FormWindowState.Normal;
                    }
                    f.Activate();
                    return true;
                }
            }
            return false;
        }
        private void ReLogin_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("LoginForm") == true)
            {
                return;
            }
            else
            {
                LoginForm fl = new LoginForm();
                fl.MdiParent = this;
                fl.Show();
            }
        }

        private void DepartmentQuery_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmDepartmentMgnt") == true)
            {
                return;
            }
            else
            {
                frmDepartmentMgnt departmentMgnt = new frmDepartmentMgnt();
                departmentMgnt.MdiParent = this;
                departmentMgnt.Show();
            }
        }

        private void CousrInfo_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmCourseMgnt") == true)
            {
                return;
            }
            else
            {
                frmCourseMgnt cm = new frmCourseMgnt();
                cm.MdiParent = this;
                cm.Show();
            }
        }

        private void TeacherQuery_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmTeacherInfo") == true)
            {
                return;
            }
            else
            {
                frmTeacherInfo teacherInfo = new frmTeacherInfo(String.Empty, String.Empty);
                teacherInfo.MdiParent = this;
                teacherInfo.Show();
            }
        }

        private void TeacherCourse_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmTeacherInfo") == true)
            {
                return;
            }
            else
            {
                frmOpenCourseInfo tocInfo = new frmOpenCourseInfo(String.Empty, String.Empty);
                tocInfo.MdiParent = this;
                tocInfo.Show();
            }
        }

        private void 学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmStudentMgnt") == true)
            {
                return;
            }
            else
            {
                frmStudentMgnt student = new frmStudentMgnt(String.Empty);
                student.MdiParent = this;
                student.Show();
            }
        }

        private void IdleClassRmQuery_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmClassroomToUse") == true)
            {
                return;
            }
            else
            {
                frmClassroomToUse fcr = new frmClassroomToUse();
                fcr.MdiParent = this;
                fcr.Show();
            }

        }

        private void ClassRmInfo_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmClassroomInfo") == true)
            {
                return;
            }
            else
            {
                frmClassroomInfo fcr = new frmClassroomInfo();
                fcr.MdiParent = this;
                fcr.Show();
            }
        }

        private void ApplyForFrR_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmFreeClassroomQuery") == true)
            {
                return;
            }
            else
            {
                frmFreeClassroomQuery freecr = new frmFreeClassroomQuery();
                freecr.MdiParent = this;
                freecr.Show();
            }
        }

        private void 帮助ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("frmAboutUsInfo") == true)
            {
                return;
            }
            else
            {
                frmAboutUsInfo freecr = new frmAboutUsInfo();
                freecr.MdiParent = this;
                freecr.Show();
            }
        }

        private void 教室申请ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.CheckForm("ApplyForClassRoom"))
            {
                return;
            }
            else
            {
                frmApplyForRoom frmRoom = new frmApplyForRoom();
                frmRoom.MdiParent = this;
                frmRoom.Show();
            }
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            switch (userRight)
            {
                case "管理员":
                    {
                        if (studentRep.NotAgree())
                        {
                            DialogResult result = MessageBox.Show("有新的教室申请未审批,是否现在进行审批？", "审批提示", MessageBoxButtons.OKCancel);
                            if (result == DialogResult.OK)
                            {
                                this.IdleClassRmQuery_Click(sender, e);
                            }
                        }
                        break;
                    }
                default:
                    {
                        if (studentRep.Prompt(userPass))
                        {
                            MessageBox.Show("管理员已经同意了您的教室申请,借用时间结束之后，该借用信息将被删除！");
                        }
                        break;
                    }
            }
        }
    }
}
